package com.mjf.srm.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mjf.srm.config.SRMConfig;
import com.mjf.srm.enums.SRMApiEnum;
import com.mjf.srm.util.SRMSendUtil;
import com.mjf.srm.vo.PURInfo2VO;
import com.mjf.srm.vo.PURInfoVO;
import com.mjf.srm.vo.PURListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/6/29 01:12
 */
@Slf4j
@Service
public class SRMAsyncService {

    @Autowired
    private SRMConfig srmConfig;

    /**
     * 获取采购订单列表 及明细
     */
    public List<PURInfoVO.PURVO> asyncPurchaseOrder() {
        List<PURInfoVO.PURVO> purs = new ArrayList<>();
        try {
            String sign = SRMSendUtil.getSupplierLoginCode(srmConfig.getUrl() + SRMApiEnum.VENDOR_VERIFY.getUrl(),
            srmConfig.getSupplierCode(),
                srmConfig.getEncryptPrefix());
            JSONObject jsonObject = new JSONObject();
            jsonObject.putOpt("verify_code", sign);
            JSONObject result = SRMSendUtil.sendPost(srmConfig.getUrl() + SRMApiEnum.PUR_LIST.getUrl(), jsonObject);
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
            assert result != null;
            PURListVO vo = objectMapper.readValue(result.toString(), PURListVO.class);
            for (PURListVO.PURVO purVO : vo.getData().getPURS().getPUR()) {
                jsonObject.putOpt("pur_header_id", purVO.getPur_header_id());
                result = SRMSendUtil.sendPost(srmConfig.getUrl() + SRMApiEnum.PUR_FIND.getUrl(), jsonObject);
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                assert result != null;
                try {
                    PURInfoVO vo1 = objectMapper.readValue(result.toString(), PURInfoVO.class);
                    if (CollectionUtil.isNotEmpty(vo1.getData().getPUR())) {
                        vo1.getData().getPUR().forEach(purVO1 -> {
                            purVO1.setPUR_HEADER_ID(purVO.getPur_header_id());
                        });
                        purs.addAll(vo1.getData().getPUR());
                    }
                } catch (Exception e) {
                    PURInfo2VO vo1 = objectMapper.readValue(result.toString(), PURInfo2VO.class);
                    if (ObjectUtil.isNotNull(vo1.getData().getPUR())) {
                        vo1.getData().getPUR().setPUR_HEADER_ID(purVO.getPur_header_id());
                        purs.add(vo1.getData().getPUR());
                    }
                }
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            log.error("获取采购订单列表 及明细 失败 ", e);
        }
        return purs;
    }

}
